using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using Common;
using CustomChart;
using Infragistics.UltraChart.Data.Series;
using Infragistics.UltraChart.Resources.Appearance;
using SProject;
using SProject.Paramter;
using SWellPlate;

namespace MainView.MainControls.Chart
{
	public class AMPCurveChartForThreshold : AMPCurveChart
	{
		private const string KeyThreshold = "Threshold";

		public static readonly RoutedEvent OnThresholdChangedRoutedEvent = EventManager.RegisterRoutedEvent("OnThresholdChangedRoutedEvent", RoutingStrategy.Bubble, typeof(EventHandler<RoutedEventArgs>), typeof(AMPCurveChartForThreshold));

		public List<int> channelList = new List<int>();

		public event RoutedEventHandler OnThresholdChanged
		{
			add
			{
				AddHandler(OnThresholdChangedRoutedEvent, value);
			}
			remove
			{
				RemoveHandler(OnThresholdChangedRoutedEvent, value);
			}
		}

		protected override void CreateThresholdSeries(BasicOptionItem item, ChartData chartData)
		{
			RegisterSubsetParamterEvent(chartData);
			XYSeriesEx xYSeriesEx = _ThresholdSeriesList.Where((XYSeriesEx s) => s.Channel == item.ChannelNo).FirstOrDefault();
			if (item != null && xYSeriesEx == null)
			{
				item.Event += base.BasicOptionItem_Event;
				double threshold = item.Threshold;
				xYSeriesEx = new XYSeriesEx();
				xYSeriesEx.Visible = false;
				xYSeriesEx.PEs.Add(new PaintElement());
				xYSeriesEx.PEs[0].Fill = GetThresholdSeriesColor(item.ChannelNo);
				xYSeriesEx.Tag = item;
				xYSeriesEx.Channel = item.ChannelNo;
				xYSeriesEx.Points.Add(new XYDataPoint(base.MinX, threshold, "", false));
				xYSeriesEx.Points.Add(new XYDataPoint(base.MaxX, threshold, "", false));
				_ThresholdSeriesList.Add(xYSeriesEx);
				base.UltraChart.AddThresholdSeries(xYSeriesEx);
			}
		}

		protected override void ResetThresholdSeriesPoint()
		{
			foreach (XYSeriesEx thresholdSeries in _ThresholdSeriesList)
			{
				XYDataPointCollection points = thresholdSeries.Points;
				if (points.Count > 1)
				{
					points[0].ValueX = base.UltraChart.GetCurrentMinX();
					points[1].ValueX = base.UltraChart.GetCurrentMaxX();
				}
			}
		}

		protected override bool GetSeriesVisible(ChartData chartData)
		{
			return channelList.Contains(chartData.SampleTargetItem.ChannelNo);
		}

		protected override void RefreshThresholdValue(BasicOptionItem item, string property)
		{
			if (property != "Threshold")
			{
				return;
			}
			XYSeriesEx xYSeriesEx = _ThresholdSeriesList.Where((XYSeriesEx s) => s.Tag == item).FirstOrDefault();
			if (xYSeriesEx != null)
			{
				XYDataPointCollection points = xYSeriesEx.Points;
				for (int i = 0; i < points.Count; i++)
				{
					points[i].ValueY = item.Threshold;
				}
				if (xYSeriesEx.Visible)
				{
					xYSeriesEx.Visible = false;
					xYSeriesEx.Visible = true;
				}
				RaiseItemUpdateEvent();
			}
		}

		protected override string GetThresholdToolTips(XYSeriesEx series)
		{
			if (series.Tag is BasicOptionItem)
			{
				BasicOptionItem basicOptionItem = (BasicOptionItem)series.Tag;
				return basicOptionItem.TargetName + Utility.GetMessage("Colon") + basicOptionItem.Threshold.ToString("n2");
			}
			return base.GetThresholdToolTips(series);
		}

		protected override void SetThreshold(XYSeriesEx series, int x, int y)
		{
			if (!(series.Tag is BasicOptionItem))
			{
				return;
			}
			BasicOptionItem item = (BasicOptionItem)series.Tag;
			item.Threshold = base.UltraChart.GetLocationYData(y);
			List<Project> allProject = base.Subset.GetAllProject();
			foreach (Project item2 in allProject)
			{
				List<BasicOptionItem> list = base.Subset.GetSubsetParamter(item2).BasicOption.Items.Where((BasicOptionItem s) => s.ChannelNo == item.ChannelNo).ToList();
				if (list == null)
				{
					continue;
				}
				foreach (BasicOptionItem item3 in list)
				{
					item3.Threshold = item.Threshold;
				}
			}
			RaiseEvent(new RoutedEventArgs
			{
				RoutedEvent = OnThresholdChangedRoutedEvent,
				Source = this
			});
		}

		protected override bool CanThresholdChanged(BasicOptionItem item)
		{
			return true;
		}
	}
}
